package com.fm.common.base

import androidx.viewbinding.ViewBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.fm.common.R
import com.fm.common.databinding.FragmentBaseRecyclerBinding
import com.fm.common.entity.BasePageEntity
import com.fm.common.utils.setEmptyView
import com.fm.common.utils.setLinearLayoutManager
import com.fm.common.wediget.EmptyLayout

/**
 *
 * @Author:         ly2
 * @CreateDate:     2021/1/13
 * @Description:
 *
 */
abstract class BaseRecyclerFragmentP<B : ViewBinding, D, P : BasePresenter<*>> : BaseBPFragment<B, P>() {
    /**
     * 网络请求当前页码
     */
    protected var page = 1

    /**
     * 是否有下一页
     */
    protected var isHasNext = false

    /**
     * 每次请求分页数量
     */
    protected var row = 20

    /**
     * 列表适配器
     */
    protected var mAdapter: BaseQuickAdapter<D, BaseViewHolder>? = null

    /**
     * 请求数据为空时，空布局类型
     */
    protected var emptyType = EmptyLayout.EmptyType.DEFAULT


    /**
     * 初始化刷新
     * @param enable 刷新控件是否可用
     */
    protected fun initRefresh(enable: Boolean = true) {
        binding?.let { bind ->
            bind as FragmentBaseRecyclerBinding
            if (enable) {
                bind.fbSrlRoot.let { sw ->
                    sw.isRefreshing = true
                    sw.setColorSchemeColors(*resources.getIntArray(R.array.refresh_color))
                    sw.setOnRefreshListener {
                        page = 1
                        request()
                    }
                }
            } else {
                bind.fbSrlRoot.isEnabled = false
            }
        }
    }

    override fun initData() {
        super.initData()
        request()
    }

    /**
     * 初始化Adapter
     * @param loadMore  列表是否加载更多可用
     * @param space     列表item间分割size
     */
    protected fun initAdapter(loadMore: Boolean = false, space: Int = 0) {
        binding?.let { bind ->
            bind as FragmentBaseRecyclerBinding
            mAdapter = getAdapter()
            bind.fbRlvContent.setLinearLayoutManager(mContext!!, space)
            bind.fbRlvContent.adapter = mAdapter
            mAdapter?.let { adapter ->
                adapter.isAnimationFirstOnly = true
                adapter.animationEnable = true
                adapter.setAnimationWithDefault(BaseQuickAdapter.AnimationType.ScaleIn)
                if (loadMore) {
                    adapter.loadMoreModule.let {
                        it.setOnLoadMoreListener {
                            bind.fbRlvContent.post {
                                if (isHasNext) {
                                    page++
                                    request()
                                } else {
                                    it.loadMoreEnd()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 解析列表数据
     */
    protected fun parseData(pageBean: BasePageEntity<D>?) {
        (binding as FragmentBaseRecyclerBinding).fbSrlRoot.isRefreshing = false
        if (null == pageBean) {
            mAdapter?.setEmptyView(mContext!!, EmptyLayout.EmptyType.ERROR){
                request()
            }
            return
        }
        if (pageBean.records.isNullOrEmpty()) {
            if (pageBean.current == 1) {
                mAdapter?.setNewInstance(null)
                mAdapter?.setEmptyView(mContext!!, emptyType)
            } else {
                mAdapter?.loadMoreModule?.loadMoreFail()
            }
        } else {
            if (pageBean.current == 1) {
                mAdapter?.setNewInstance(pageBean.records as MutableList<D>?)
            } else {
                mAdapter?.addData(pageBean.records!!)
                mAdapter?.loadMoreModule?.loadMoreComplete()
            }
        }
        isHasNext = pageBean.hasNext
    }

    /** 请求网络数据  */
    protected open fun request() {
    }

    /**
     * 初始化adapter
     */
    protected abstract fun getAdapter(): BaseQuickAdapter<D, BaseViewHolder>
}